package com.game.server.db.entity;

import com.game.majiang.config.DataManager;
import com.game.server.message.IRespose;
import com.game.server.util.CompuUtils;
import lombok.Getter;
import lombok.Setter;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Getter
@Setter
public class XyUnionInfo {

    public int id; //联盟ID
    public String union_name; //联盟昵称
    public String union_head; // 联盟头像
    public int union_integral; //联盟剩余额度
    public int creator_clubid; //联盟创建者俱乐部ID
    public int ratio; //联盟成员贡献比例
    public Timestamp createdate; //联盟创建时间
    public List<Integer> clubids = new ArrayList<>();//联盟下的所有俱乐部id


    /**
     * 获取联盟人数
     * @return
     */
    public int getPlayerCount() {
        int count = 0;
        for (Integer clubid : clubids) {
            XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(clubid);
            count += clubInfo.getPlayerCount();
        }
        return count;
    }

    /**
     * 联盟战绩结算
     * @param tableUserInfo 桌子内的玩家信息
     */
    public synchronized void unionRecordSettlement(TableUserInfo tableUserInfo){
        if (tableUserInfo.getTableUnionID()!=0 && tableUserInfo.getTableUnionID()==this.getId()){
            List<TableUser> tableUser = tableUserInfo.getTableUser();
            for (TableUser user : tableUser) {
                Integer cid = user.getCid();
                int win = user.getBalance() - user.getDairu();
                if (cid!=null && cid!=0){
                    if (win>0){
                        BigDecimal totalContribution = CompuUtils.multiply(win, Double.parseDouble(CompuUtils.divide(this.ratio+"",100+"").toString()));
                        System.out.println("联盟成员贡献比例:"+totalContribution);
                        //联盟成员贡献比例
                        int deduct = (int) Math.floor(Double.valueOf(totalContribution.toString()));//扣除
                        System.out.println("deduct联盟成员贡献比例:"+deduct);
                        int handBack = win - deduct;//退还贡献
                        System.out.println("handBack联盟成员贡献比例:"+handBack);
                        XyClubInfo info = DataManager.getInstance().getMapClub().get(cid);
                        if (handBack>=0){
                            info.updateUnionedu(4, (handBack+user.dairu), this.getId(), 0, tableUserInfo.getTableNo(),tableUserInfo.getGamekey());
                        }
                        if (deduct>=0){
                            BigDecimal divideTheContribution = CompuUtils.multiply(deduct, 0.5);//输家赢家各一半
                            int half = (int) Math.floor(Double.valueOf(divideTheContribution.toString()));//输家赢家各一半
                            if (half>=0){
                                info.updateUnionedu(5, half, this.getId(), 0, tableUserInfo.getTableNo(),tableUserInfo.getGamekey());
                            }
                        }
                    }else{
                        win = Math.abs(win);
                        XyClubInfo info = DataManager.getInstance().getMapClub().get(cid);
                        int retreatBobo = user.getDairu() - win;
                        if (retreatBobo>0){
                            info.updateUnionedu(4, retreatBobo, this.getId(), 0, tableUserInfo.getTableNo(),tableUserInfo.getGamekey());
                        }
                        BigDecimal totalContribution = CompuUtils.multiply(win, Double.parseDouble(CompuUtils.divide(this.ratio+"",100+"").toString()));//联盟成员贡献比例
                        int deduct = (int) Math.floor(Double.valueOf(totalContribution.toString()));//扣除
                        if (deduct>=0){
                            BigDecimal divideTheContribution = CompuUtils.multiply(deduct, 0.5);//输家赢家各一半
                            int half = (int) Math.floor(Double.valueOf(divideTheContribution.toString()));//输家赢家各一半
                            if (half>=0){
                                info.updateUnionedu(5, half, this.getId(), 0, tableUserInfo.getTableNo(),tableUserInfo.getGamekey());
                            }
                        }
                    }
                }
            }

        }
    }

    //广播给联盟所有人
    public void broadCastPlayer(IRespose rsp) {
        if (rsp != null) {
            List<Integer> clubids = getClubids();
            for (Integer clubid : clubids) {
                XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(clubid);
                clubInfo.broadCastPlayer(rsp);
            }
        }
    }
    //广播给联盟所有管理员以及盟主
    public void broadCastAdminPlayer(IRespose rsp) {
        if (rsp != null) {
            List<Integer> clubids = getClubids();
            for (Integer clubid : clubids) {
                XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(clubid);
                clubInfo.broadCastAdminPlayer(rsp);
            }
        }
    }

}
